package com.beiyou.controller;

import cn.hutool.core.util.StrUtil;
import com.beiyou.model.UserFace;
import com.beiyou.model.UserFaceCheck;
import com.beiyou.service.MyFaceEngine;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: King-lby
 * @Date Created in 2022-12-05 9:55
 */
@RestController
@RequestMapping("/face-api/face")
public class FaceController {
    private MyFaceEngine myFaceEngine;

    @Resource(name = "bytesHashOperations")
    private HashOperations<String, String, byte[]> hashOperations;

    @Resource
    private StringRedisTemplate stringRedisTemplate;


    //采集人脸
    @PostMapping("/up")
    public Integer upload(@RequestBody UserFace userFace) {

        String base64String = userFace.getFaceImg();

        String[] dataArray = StrUtil.splitToArray(base64String, "base64,");

        byte[] bytes = Base64.decode(dataArray[1]);

        byte[] feature = myFaceEngine.getFeature(bytes);

        String daKey = "venue." + userFace.getVenueId();
        String xiaoKey = userFace.getId().toString();
        hashOperations.put(daKey, xiaoKey, feature);

        return 1;
    }

    //校验人脸
    @PostMapping("/check")
    public String check(@RequestBody UserFaceCheck userFaceCheck) {
        List<String> daKeys = new ArrayList<>();
        /**
         * 由于keys 命令可能会阻塞Redis，
         * 一般情况下生产环境都会禁止使用。所以可以采用 scan 命令遍历一些需要模糊匹配key的场景
         */
        //scan需要匹配的key，模糊匹配 找到所有 以 venue.开头的key
        ScanOptions scanOptions = ScanOptions.scanOptions().match("venue.*").build();
        //获取游标
        Cursor<String> cursor = stringRedisTemplate.scan(scanOptions);
        while (cursor.hasNext()){
//            //获取游标id
//            System.out.println(cursor.getCursorId());
            System.out.println(cursor.next());
        }
        //关闭
        cursor.close();

        String base64String = userFaceCheck.getFaceImg();
        String[] dataArray = StrUtil.splitToArray(base64String, "base64,");
        byte[] bytes = Base64.decode(dataArray[1]);
        byte[] feature = myFaceEngine.getFeature(bytes);

        //人脸校验开始
        for (String dakey : daKeys){
            Map<String, byte[]> entries = hashOperations.entries(dakey);
            for (Map.Entry<String, byte[]> entry : entries.entrySet()) {
                float score = myFaceEngine.compare(feature, entry.getValue());
                if (score > 0.90) {
                    return entry.getKey();
                }
            }
        }
        return "nobody";
    }
}
